﻿
/*
 * Copyright (C) Han Wei
 */


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using log4net;
using BSvr.Model;

namespace BSvr.Server
{
    class BatchProcess : BatchChannel.Iface
    {
        private static ILog loger = log4net.LogManager.GetLogger(typeof(BatchProcess));

        private Server _server = null;

        public BatchProcess(Server server)
        {
            this._server = server;
        }

        #region Iface 成员
        /// <summary>
        /// 执行方法
        /// </summary>
        /// <param name="handlerno">业务编号</param>
        /// <param name="cmd">命令</param>
        /// <param name="cmdarg">命令参数</param>
        /// <returns>结果</returns>
        public Result Excute(string handlerno, string cmd, List<string> cmdarg)
        {
            Result result = new Result();
            if (this._server.Plugins.Keys.Contains(handlerno))
            {
                PluginDomain excDm = this._server.Plugins[handlerno];
                if (excDm.Loaded && excDm.Running)
                { // 判断是否加载或执行
                    try
                    {
                        OpRst<string> esrt = excDm.Proxy.Excute(cmd, cmdarg);
                        result.IsSuccess = esrt.IsSuccess;
                        result.OpResult = esrt.Result;
                    }
                    catch (Exception ex) {
                        loger.Error(ex);
                        result.IsSuccess = false;
                        result.OpResult = Error.Serialize(Error.EXCEPTION,"业务"+handlerno+"执行"+cmd+"时异常，异常原因："+ex.Message);
                    }
                }
                else {
                    result.IsSuccess = false;
                    result.OpResult = Error.Serialize(Error.UN_RUNNING, "没有该业务");
                }
            }
            else
            {
                result.IsSuccess = false;
                result.OpResult = Error.Serialize(Error.NO_BUS,"没有该业务");
            }
            return result;
        }

        /// <summary>
        /// 查询方法
        /// </summary>
        /// <param name="handlerno">业务编号</param>
        /// <param name="query">查询</param>
        /// <param name="queryarg">查询参数</param>
        /// <returns></returns>
        public Result Query(string handlerno, string query, List<string> queryarg)
        {
            Result result = new Result();
            if (this._server.Plugins.Keys.Contains(handlerno))
            {
                PluginDomain excDm = this._server.Plugins[handlerno];
                if (excDm.Loaded && excDm.Running)
                { // 判断是否加载或执行
                    try
                    {
                        OpRst<string> esrt = excDm.Proxy.Query(query, queryarg);
                        result.IsSuccess = esrt.IsSuccess;
                        result.OpResult = esrt.Result;
                    }
                    catch (Exception ex)
                    {
                        loger.Error(ex);
                        result.IsSuccess = false;
                        result.OpResult = Error.Serialize(Error.EXCEPTION,"业务" + handlerno + "执行" + query + "时异常，异常原因：" + ex.Message);
                    }
                }
                else
                {
                    result.IsSuccess = false;
                    result.OpResult = Error.Serialize(Error.UN_RUNNING, "没有该业务");
                }
            }
            else
            {
                result.IsSuccess = false;
                result.OpResult = Error.Serialize(Error.NO_BUS, "没有该业务");
            }
            return result;
        }

        #endregion
    }
}
